package com.yxs.school.api.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yxs.school.api.entity.File;
import com.yxs.school.api.service.IFileService;
import com.yxs.school.result.R;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 文件 前端控制器
 * </p>
 *
 * @author whp
 * @since 2019-11-15
 */
@RestController
@RequestMapping("/api/file")
public class FileController {
    @Resource
    private IFileService fileService;
    @PostMapping("save")
    public R save(@RequestBody File file) {
        boolean flag = fileService.save(file);
        if (flag) {
            return R.data(200,null,"成功");
        }else {
            return R.fail(400,"失败");
        }
    }
    @PostMapping("update")
    @PutMapping("update")
    public R update(@RequestBody File file) {
        boolean flag = fileService.updateById(file);
        if (flag ){
            return R.data(200,null,"成功");
        }else {
            return R.fail(400,"失败");
        }
    }
    @PostMapping("delete")
    @DeleteMapping("delete")
    public R delete(@RequestBody List<Integer> ids) {
        boolean flag =fileService.removeByIds(ids);
        if (flag ){
            return R.data(200,null,"成功");
        }else {
            return R.fail(400,"失败");
        }
    }
    @PostMapping("deleteOne")
    @DeleteMapping("deleteOne")
    public R deleteOne(Integer id) {
        boolean flag =fileService.removeById(id);
        if (flag ){
            return R.data(200,null,"成功");
        }else {
            return R.fail(400,"失败");
        }
    }
    @GetMapping("list")
    public R list(Integer articleId,String fileName,String upTime,String fileType,String uploader) {
        QueryWrapper<File> wrapper = new QueryWrapper<>();
        if (!"".equals(fileName)&&fileName !=null) {
            wrapper.like("file_name",fileName);
        }
        if (!"".equals(fileType)&&fileType !=null) {
            wrapper.eq("type",fileType);
        }
        if (!"".equals(uploader)&&uploader !=null) {
            wrapper.eq("upload_by",uploader);
        }
        List<File> list= fileService.list(wrapper);
        return R.data(200,list,"成功");
    }
    @GetMapping("selectList")
    public R selectList(Integer articleId,String fileName,String upTime,String fileType,String uploader) {
        System.out.println("------");
        QueryWrapper<File> wrapper = new QueryWrapper<>();
//        wrapper.select("id","size","file_name");
        if (!"".equals(fileName)&&fileName !=null) {
            wrapper.like("file_name",fileName);
        }
        if (!"".equals(fileType)&&fileType !=null) {
            wrapper.eq("type",fileType);
        }
        if (!"".equals(uploader)&&uploader !=null) {
            wrapper.eq("upload_by",uploader);
        }
        List<File> list= fileService.selectListWr(wrapper);
        return R.data(200,list,"成功");
    }
}
